#!/bin/sh

# Author : Jagadish Manchala
# If You Have any Issues Executing the Scripts , Dont Look For Me :-)


# description: Control Script For Starting , Stopping and Restarting the OEM
# processname: java
# pidfile: /var/run/oem-agent.pid

# The Start,Stop and Recycle Of OEM also Include Oms Agent.So If we restart,start or stop oem we need to make sure that OMS is handled before the OEM.



# ----------------------------------------------------------------------
# Directory Locations Necessary For Execution of the Commands 
# ----------------------------------------------------------------------

#AGENT_INSTANCE="/software/oem/agentinstall/agent_inst"
AGENT_PIDFILE_DIR="${OEM_SCRIPTSDIR}/bin"

# ----------------------------------------------------------------------
# Function that prepares the location where the pidfile will live
# ----------------------------------------------------------------------


prepare_pid_dir ()
{
   mkdir -p "$AGENT_PIDFILE_DIR"
 
   if [ "$?" != "0" ]; then
      echo "Cannot create the directory where the pidfile will go: ${AGENT_PIDFILE_DIR}"
      exit 1
   fi

}

# -------------------------------
# Find out where to put the pidfile and prepare its directory
#--------------------------------

OMS_PIDFILE="${AGENT_PIDFILE_DIR}/oms-agent.pid"
touch $OMS_PIDFILE

# ----------------------------------------------------------------------
# Function that ensures that the pidfile no longer exists
# ----------------------------------------------------------------------

remove_pid_files ()
{
   if [ -f "${OMS_PIDFILE}" ]; then
      rm "${OMS_PIDFILE}"
   fi
   
   if [ -f "${OEM_PIDFILE}" ]; then
      rm "${OEM_PIDFILE}"
   fi 
   
}


remove_pid_oms ()
{
   if [ -f "${OMS_PIDFILE}" ]; then
      rm "${OMS_PIDFILE}"
   fi

}

remove_pid_oem ()
{

   if [ -f "${OEM_PIDFILE}" ]; then
      rm "${OEM_PIDFILE}"
   fi

}


# ----------------------------------------------------------------------
# Function that sets _STATUS, _RUNNING and _PID based on the status of
# the RHQ Agent process as found in the pidfile
# ----------------------------------------------------------------------

check_status_of_pid ()
{

    if [ -n "$1" ] && kill -0 $1 2>/dev/null ; then
       OMS_STATUS="OMS Agent (pid $1) is running"
       OMS_RUNNING=1
    else
       OMS_STATUS="OMS Agent (pid $1) is NOT running"
       OMS_RUNNING=0
    fi

}



check_status ()
{
    if [ -f "${OMS_PIDFILE}" ]; then
        OMS_PID=`cat "${OMS_PIDFILE}"`
        check_status_of_pid $OMS_PID
    else
        OMS_STATUS="OMS Agent (no pidfile) is NOT running"
        OMS_RUNNING=0
    fi
	
}


# -------------------------------
# Determine where this script is, and change to its directory

cd "$RHQ_AGENT_WRAPPER_BIN_DIR_PATH"
_THIS_SCRIPT="${AGENT_INSTANCE}/bin/emctl"


# --------------------------------------------------------------------------
# Main Processing Starts here
# check_status Is Executed First To get the Current Stats
#---------------------------------------------------------------------------

check_status

case "$1" in

'start')
     prepare_pid_dir

     echo "Starting the Agent Now"

     #Check For the Status Of OMS 
     if [ "$OMS_RUNNING" = "1" ]; then
           echo $OMS_STATUS
           exit 0
        fi

      OMS_AGENT_IN_BACKGROUND="${OMS_PIDFILE}"
      export OMS_AGENT_IN_BACKGROUND
  
      echo "Starting OMS Agent..."
     
     #Determine the command to execute when starting the agent
       if [ -z "$OMS_AGENT_START_COMMAND" ]; then
          _START_SCRIPT="${AGENT_INSTANCE}/bin/emctl"

         if [ ! -f "$_START_SCRIPT" ]; then
             echo "ERROR! Cannot find the OMS Agent start script"
             echo "Not found: $_START_SCRIPT"
             exit 1
       fi

      OMS_AGENT_START_COMMAND="${_START_SCRIPT} start agent"
      fi

      echo "Script Getting Executed is $OMS_AGENT_START_COMMAND"
  
      # Execute the Script To Start the agent now!
      . $OMS_AGENT_START_COMMAND > "/logs/oem/oms.log" 2>&1 & 
   
      sleep 1m
    
      OMS_AGENT_BACKGROUND_PID=$($AGENT_INSTANCE/bin/emctl status agent | grep 'Agent Process ID' | cut -d':' -f2 | tr -d ' ')


      if [ "$OMS_AGENT_IN_BACKGROUND" != "nofile" ]; then
           echo $OMS_AGENT_BACKGROUND_PID > "$OMS_AGENT_IN_BACKGROUND"
       fi

      check_status
      echo $OMS_STATUS

      if [ "$OMS_RUNNING" = "1" ]; then
         exit 0
      else
         echo "Failed to start - make sure the  Agent is fully configured properly"
         exit 1
      fi


;;

# ----------------------- Start Function Complete ------------------------
'stop')
 
        prepare_pid_dir

        if [ "$OMS_RUNNING" = "0" ]; then
           echo $OMS_STATUS
           remove_pid_oms
           exit 0
        fi

        echo "Stopping OMS Agent..."

        _PID_TO_KILL=$OMS_PID;

        echo "OMS Agent (pid=${_PID_TO_KILL}) is stopping..."

        #Determine the command to execute when starting the agent
       if [ -z "$OMS_AGENT_STOP_COMMAND" ]; then
          _STOP_SCRIPT="${AGENT_INSTANCE}/bin/emctl"

         if [ ! -f "$_STOP_SCRIPT" ]; then
             echo "ERROR! Cannot find the OMS Agent stop script"
             echo "Not found: $_STOP_SCRIPT"
             exit 1
       fi

      OMS_AGENT_STOP_COMMAND="${_STOP_SCRIPT} stop agent"
      fi

      echo "Script Getting Executed is $OMS_AGENT_STOP_COMMAND"

      # Execute the Script To Start the agent now!
      . $OMS_AGENT_STOP_COMMAND > "/logs/oem/oms.log" 2>&1 &

      sleep 1m

     check_status_of_pid $_PID_TO_KILL

      if [ "$OMS_RUNNING" = "1" ]; then
          kill -9 $_PID_TO_KILL
      fi

      while [ "$OMS_RUNNING" = "1" ]; do
          sleep 2
          check_status_of_pid $_PID_TO_KILL
       done

       remove_pid_oms
       echo "OMS Agent has stopped."
       exit 0 
;;


'status')

        echo $OMS_STATUS
        if [ "$OMS_RUNNING" = "0" ]; then
           exit 1
        fi
        exit 0

;;

'restart')
        echo -e "Stopping the Agent..."
        "${_THIS_SCRIPT}" stop agent > /dev/null 2>&1
        sleep 50
        echo -e "Agent Stopped Sucessfully"

        echo -e "Starting the Agent Now ..."
        "${_THIS_SCRIPT}" start agent > /dev/null 2>&1
        sleep 1m
          
        OMS_AGENT_IN_BACKGROUND="${OMS_PIDFILE}"
        export OMS_AGENT_IN_BACKGROUND
   
        OMS_AGENT_BACKGROUND_PID=$(/software/oem/agentinstall/agent_inst/bin/emctl status agent | grep 'Agent Process ID' | cut -d':' -f2 | tr -d ' ')
        remove_pid_oms

       if [ "$OMS_AGENT_IN_BACKGROUND" != "nofile" ]; then
           echo $OMS_AGENT_BACKGROUND_PID > "$OMS_AGENT_IN_BACKGROUND"
       fi

        echo -e "Agent Started Succssfully...." 
        exit $?
        ;;
*)
        echo "Usage: $0 { start | stop | restart | status }"
        exit 1
        ;;
esac


